import torch
from pylab import *
import datetime
import json
from mongoapi import finddb
device = torch.device('cpu')


def joinKline(data, num):
    if num == 1:
        return data
    start = int(data[0]['Date']) % (num * 60)
    startint = int(start / 60)
    startint = num - startint
    data = data[startint:]
    newdata = []
    addarr = []
    starttime = int(data[0]['Date'])
    endtime = starttime + num * 60
    for i in data:
        if int(i["Date"]) > endtime:
            temp = {
                "Date": starttime,
                "Open": addarr[0]["Open"],
                "High": max([one["High"] for one in addarr]),
                "Low": min([one["Low"] for one in addarr]),
                "Close": addarr[-1]["Close"],
            }
            newdata.append(temp)
            time_difference = (int(i['Date']) % (num * 60))

            starttime = int(i['Date']) - time_difference
            endtime = starttime + num * 60
            addarr = []
            addarr.append(i)
        if int(i["Date"]) >= starttime and int(i["Date"]) <= endtime:
            addarr.append(i)
    AddDI(newdata)
    return newdata


def AddDI(datalist):
    for one in datalist:
        one["DI"] = (one["Close"] * 2 + one["Low"] + one["High"]) / 4


def get_EMA(df, a=0.7):
    ema = df
    for i in range(len(df)):
        if i == 0:
            ema[i] = df[i]
        if i > 0:
            ema[i] = (1 - a) * ema[i - 1] + a * df[i]
    return ema


def savedata(symbol="FXUSDJPY"):
    print(symbol, '  start!\t', datetime.datetime.now())
    datalist = finddb(dbname="FX_CM", tablename=symbol, limit=20000000)
    # datalist = json.load(open("data/%s.json" % symbol, 'r'))
    with open("data/N%s.json" % symbol, 'w') as f:
        json.dump(datalist, f)
    print(symbol, '  end!\t', datetime.datetime.now())


if __name__ == "__main__":
    savedata("USDJPY")
